﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using User.Api.Common;
using User.Api.Models;
using AutoMapper;

namespace User.Api.Controllers
{
    /// <summary>
    /// 身份证接口
    /// </summary>
    public class IDCardController : BaseApiController
    {
        /// <summary>
        /// 添加接口
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        [HttpPost]
        public ResultData<ResponseCommon<int>> Add(ReqIDCardAdd reqInfo)
        {
            var result = ResultData<ResponseCommon<int>>.Error("");
            if (reqInfo.IDNum.IsEmpty())
            {
                throw new BizException(StatusCodeEnum.IDCardNumNotNull);
            }
            if (!ValidateHelper.IsIdCard(reqInfo.IDNum))
            {
                throw new BizException(StatusCodeEnum.IDCardIdNumWrong);
            }
            if (reqInfo.Name.IsEmpty())
            {
                throw new BizException(StatusCodeEnum.IDCardNameNotNull);
            }
            if (reqInfo.UserID <= 0)
            {
                throw new BizException(StatusCodeEnum.IDCardUserIDNotNull);
            }
            var oExist = IDCardOperate.GetByUserIDAndNum(reqInfo.UserID, reqInfo.IDNum);
            if (oExist != null)
            {
                throw new BizException(StatusCodeEnum.IDCardExist);
            }
            var oInfo = Mapper.Map<IDCardInfo>(reqInfo);
            oInfo.IsActive = 1;
            oInfo.CreatedDate = DateTime.Now;
            oInfo.UpdatedDate = DateTime.Now;
            int nInsertID = IDCardOperate.Insert(oInfo);
            result = ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(nInsertID));
            return result;
        }

        /// <summary>
        /// 添加更新接口
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        [HttpPost]
        public ResultData<ResponseCommon<int>> AddOrUpdate(ReqIDCardAdd reqInfo)
        {
            var result = ResultData<ResponseCommon<int>>.Error("");
            if (reqInfo.IDNum.IsEmpty())
            {
                throw new BizException(StatusCodeEnum.IDCardNumNotNull);
            }
            if (!ValidateHelper.IsIdCard(reqInfo.IDNum))
            {
                throw new BizException(StatusCodeEnum.IDCardIdNumWrong);
            }
            if (reqInfo.Name.IsEmpty())
            {
                throw new BizException(StatusCodeEnum.IDCardNameNotNull);
            }
            if (reqInfo.UserID <= 0)
            {
                throw new BizException(StatusCodeEnum.IDCardUserIDNotNull);
            }
            var oExist = IDCardOperate.GetByUserIDAndNum(reqInfo.UserID, reqInfo.IDNum);
            if (oExist != null)
            {
                Mapper.Map<ReqIDCardAdd, IDCardInfo>(reqInfo, oExist);
                oExist.UpdatedDate = DateTime.Now;
                IDCardOperate.Update(oExist);
                result = ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(oExist.IDCardID));
            }
            else
            {
                var oInfo = Mapper.Map<IDCardInfo>(reqInfo);
                oInfo.IsActive = 1;
                oInfo.CreatedDate = DateTime.Now;
                oInfo.UpdatedDate = DateTime.Now;
                int nInsertID = IDCardOperate.Insert(oInfo);
                result = ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(nInsertID));
            }

            return result;
        }

        /// <summary>
        /// 身份证信息更新接口
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        [HttpPost]
        public ResultData<ResponseCommon<int>> Update(ReqIDCardUpdate reqInfo)
        {
            var result = ResultData<ResponseCommon<int>>.Error();
            if (reqInfo.IDNum.IsEmpty())
            {
                throw new BizException(StatusCodeEnum.IDCardNumNotNull);
            }
            if (!ValidateHelper.IsIdCard(reqInfo.IDNum))
            {
                throw new BizException(StatusCodeEnum.IDCardIdNumWrong);
            }
            if (reqInfo.Name.IsEmpty())
            {
                throw new BizException(StatusCodeEnum.IDCardNameNotNull);
            }
            if (reqInfo.UserID <= 0)
            {
                throw new BizException(StatusCodeEnum.IDCardUserIDNotNull);
            }
            var oExist = IDCardOperate.GetByID(reqInfo.IDCardID);
            if (oExist == null)
            {
                throw new BizException(StatusCodeEnum.IDCardNotExist);
            }
            //同一身份证号不可能存在2条不同的信息
            var oExistOther =
                IDCardOperate.QuerySingle(m => m.IDCardID != reqInfo.IDCardID && m.UserID == reqInfo.UserID && m.IDNum == reqInfo.IDNum && m.IsActive == 1);
            if (oExistOther != null)
            {
                throw new BizException(StatusCodeEnum.IDCardExist);
            }
            Mapper.Map<ReqIDCardUpdate, IDCardInfo>(reqInfo, oExistOther);
            oExist.UpdatedDate = DateTime.Now;
            IDCardOperate.Update(oExist);
            result = ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(reqInfo.IDCardID));
            return result;
        }


        /// <summary>
        /// 身份证信息获取接口
        /// </summary>
        /// <param name="idCardID">身份证ID</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<ResIDCardGet> Get(int idCardID)
        {
            var result = ResultData<ResIDCardGet>.Error();
            var oInfo = IDCardOperate.GetByID(idCardID);
            if (oInfo == null)
            {
                throw new BizException(StatusCodeEnum.IDCardNotExist);
            }
            else
            {
                var oRes = Mapper.Map<ResIDCardGet>(oInfo);
                result = ResultData<ResIDCardGet>.Success(oRes);
            }
            return result;
        }


        /// <summary>
        /// 身份证信息获取接口
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<ResIDCardGet> GetByUser(long userId)
        {
            var result = ResultData<ResIDCardGet>.Error();
            var oInfo = IDCardOperate.GetByUserID(userId);
            if (oInfo == null)
            {
                throw new BizException(StatusCodeEnum.IDCardNotExist);
            }
            else
            {
                var oRes = Mapper.Map<ResIDCardGet>(oInfo);
                result = ResultData<ResIDCardGet>.Success(oRes);
            }
            return result;
        }
    }
}